<?php

class masterprofilealumni_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "master_profile_alumni";
    }
    
    private function generateId($graduate_year) {
        $query = $this->db->get_where('angkatan', array('angkatan_year' => $graduate_year));
        if ($query->num_rows() > 0) {
            $alumni_force = $query->row()->angkatan_seqno;
        }
        if (isset($alumni_force)) {
            $query = $this->db->query("select GenerateAlumniID('".$alumni_force."', '".$graduate_year."') id_alumni ");
            $row = $query->row();
            return $row->id_alumni;
        } else {
            return '';
        }
    }
    
    function insert($nis, $fullname, $birth_date, $gender, $graduate_year, $father, $mother, $home_address, $home_city, $home_phone, $member_status, $createdby) {
        $id_alumni = $this->generateId($graduate_year);
        if ($id_alumni != '') {
            $data = array(
                "mpa_id_alumni" => $id_alumni,
                "mpa_nis" => $nis,
                "mpa_fullname" => $fullname,
                "mpa_birth_date" => $birth_date,
                "mpa_gender" => $gender,
                "mpa_graduate_year" => $graduate_year,
                "mpa_father" => $father,
                "mpa_mother" => $mother,
                "mpa_home_address" => $home_address,
                "mpa_home_city" => $home_city,
                "mpa_home_phone" => $home_phone,
                "mpa_member_status" => $member_status,
                "createdby" => $createdby,
                "createddate" => date("Y-m-d H:i:s")
            );

            $this->db->insert($this->tableName, $data);
            return $this->db->insert_id();
        } else {
            return 0;
        }
    }
    
    function update($id_alumni, $data, $where = "") {
        $this->db->where("mpa_id_alumni", $id_alumni);
        if ($where != "")
            $this->db->where($where);
        return $this->db->update($this->tableName, $data);
    }
    
    function delete($id_alumni, $where = "") {
        $data = array("isdelete" => "1");
        $this->db->where("mpa_id_alumni", $id_alumni);
        if ($where != "")
            $this->db->where($where);
        return $this->db->update($this->tableName, $data);
    }

    function getById($id_alumni, $where = "") {
        $this->db->select($this->tableName.".*");
        $this->db->from($this->tableName);
        $this->db->where("mpa_id_alumni", $id_alumni);
        $this->db->where("isdelete", 0);
        if ($where != "")
            $this->db->where($where);
        return $this->db->get()->row();
    }
    
    function getAll($startpage = "0", $perpage = "10", $where = "", $like = "", $orderby = "") {
        $this->db->select($this->tableName.".*");
        if ($where != "")
            $this->db->where($where);
        if ($like != "")
            $this->db->like($like);
        $this->db->limit($perpage, $startpage);
        if ($orderby != "")
            $this->db->order_by($orderby);
        $this->db->from($this->tableName);
        $ret = $this->db->get()->result();
        log_message('debug', $this->db->last_query());
        return $ret;
    }
    
    function getAllCount($where = "", $like = "") {
        $this->db->select("COUNT(*) JUMLAH");
        $this->db->from($this->tableName);
        if ($where != "")
            $this->db->where($where);
        if ($like != "")
            $this->db->like($like);
        $ret = $this->db->get()->row()->JUMLAH;
        log_message('debug', $this->db->last_query());
        return $ret;
    }

}

?>